#include <CoreFoundation/CoreFoundation.h>
#include <Foundation/NSDictionary.h>
#include <CoreServices/CoreServices.h> 
#import <Foundation/Foundation.h>


#include "fitsio.h"

#define CHECK_RETURN_AND_SET_CFSTR_VALUE   if (status > 0) { CFStr_value=(CFStringRef)kCFNull; } else { CFStr_value = CFStringCreateWithCString(NULL,value,kCFStringEncodingASCII);}; status=0



/* -----------------------------------------------------------------------------
   Step 1
   Set the UTI types the importer supports
  
   Modify the CFBundleDocumentTypes entry in Info.plist to contain
   an array of Uniform Type Identifiers (UTI) for the LSItemContentTypes 
   that your importer can handle
  
   ----------------------------------------------------------------------------- */

/* -----------------------------------------------------------------------------
   Step 2 
   Implement the GetMetadataForFile function
  
   Implement the GetMetadataForFile function below to scrape the relevant
   metadata from your document and return it as a CFDictionary using standard keys
   (defined in MDItem.h) whenever possible.
   ----------------------------------------------------------------------------- */

/* -----------------------------------------------------------------------------
   Step 3 (optional) 
   If you have defined new attributes, update the schema.xml file
  
   Edit the schema.xml file to include the metadata keys that your importer returns.
   Add them to the <allattrs> and <displayattrs> elements.
  
   Add any custom types that your importer requires to the <attributes> element
  
   <attribute name="com_mycompany_metadatakey" type="CFString" multivalued="true"/>
  
   ----------------------------------------------------------------------------- */



/* -----------------------------------------------------------------------------
    Get metadata attributes from file
   
   This function's job is to extract useful information your file format supports
   and return it as a dictionary
   ----------------------------------------------------------------------------- */

Boolean GetMetadataForFile(void* thisInterface, 
			   CFMutableDictionaryRef attributes, 
			   CFStringRef contentTypeUTI,
			   CFStringRef pathToFile)
{

	/* 
	 *	What follows is heavily based on the example code from 
	 *  the Spotlight Developer Documentation - don't assume I 
	 *  have any idea what I'm doing!   -MDP
	 *
	 */
	
    /* Pull any available metadata from the file at the specified path */
    /* Return the attribute keys and attribute values in the dict */
    /* Return TRUE if successful, FALSE if there was no data provided */
    
	/* Pull any available metadata from the file at the specified path */
    /* Return the attribute keys and attribute values in the dict */
    /* Return true if successful, false if there was no data provided */
    Boolean success=FALSE;
    NSAutoreleasePool *pool;
        fitsfile *fptr;         /* FITS file pointer, defined in fitsio.h */
	#define MAXFILENAMELEN 1000
	char value[80],filename[MAXFILENAMELEN];
	int status=0,result=0;
	CFStringRef CFStr_value;
	char * temp;

	
    // Don't assume that there is an autorelease pool around the calling of this function.
	//    What the heck is an autorelease pool??? - MDP
    pool = [[NSAutoreleasePool alloc] init];

	temp = CFStringGetCStringPtr(pathToFile, kCFStringEncodingASCII);

	CFStringGetCString(pathToFile,filename,MAXFILENAMELEN,kCFStringEncodingASCII);


	if (!fits_open_file(&fptr, 	filename, READONLY, &status)) {

	// From the Sample Application:
	/* This may be a document which we have already imported.  It is possible that 
	   this attribute has been present before, but should no longer be (such as an 
	   MP3 file which has had its "Album" information stripped). If this value is null,
	   we should explicitly remove the attribute from the metadata by setting value
	   to kCFNull */

		// Bitpix

		result = fits_read_key(fptr,TSTRING,"COMMENT",value,NULL,&status);
		CHECK_RETURN_AND_SET_CFSTR_VALUE;
		CFDictionarySetValue(attributes,CFSTR("org_iau_FITS_COMMENT"),CFStr_value);

		// Date-obs
		/*result = fits_read_key(fptr,TSTRING,"DEC",value,NULL,&status);
		CHECK_RETURN_AND_SET_CFSTR_VALUE;
		CFDictionarySetValue(attributes,CFSTR("org_iau_FITS_DEC"),CFStr_value);
		*/

		result = fits_read_key(fptr,TSTRING,"DEC",value,NULL,&status);
		CHECK_RETURN_AND_SET_CFSTR_VALUE;
		CFDictionarySetValue(attributes,CFSTR("org_iau_FITS_DEC"),CFStr_value);

		result = fits_read_key(fptr,TSTRING,"EQUINOX",value,NULL,&status);
		CHECK_RETURN_AND_SET_CFSTR_VALUE;
		CFDictionarySetValue(attributes,CFSTR("org_iau_FITS_EQUINOX"),CFStr_value);

		result = fits_read_key(fptr,TSTRING,"EXPTIME",value,NULL,&status);
		CHECK_RETURN_AND_SET_CFSTR_VALUE;
		CFDictionarySetValue(attributes,CFSTR("org_iau_FITS_EXPTIME"),CFStr_value);
		
		result = fits_read_key(fptr,TSTRING,"HA",value,NULL,&status);
		CHECK_RETURN_AND_SET_CFSTR_VALUE;
		CFDictionarySetValue(attributes,CFSTR("org_iau_FITS_HA"),CFStr_value);
		
		result = fits_read_key(fptr,TSTRING,"FILTER",value,NULL,&status);
		CHECK_RETURN_AND_SET_CFSTR_VALUE;
		CFDictionarySetValue(attributes,CFSTR("org_iau_FITS_FILTER"),CFStr_value);

		result = fits_read_key(fptr,TSTRING,"INSTRUME",value,NULL,&status);
		CHECK_RETURN_AND_SET_CFSTR_VALUE;
		CFDictionarySetValue(attributes,CFSTR("org_iau_FITS_INSTRUME"),CFStr_value);

		result = fits_read_key(fptr,TSTRING,"OBJECT",value,NULL,&status);
		CHECK_RETURN_AND_SET_CFSTR_VALUE;
		CFDictionarySetValue(attributes,CFSTR("org_iau_FITS_OBJECT"),CFStr_value);

		result = fits_read_key(fptr,TSTRING,"OBSERVER",value,NULL,&status);
		CHECK_RETURN_AND_SET_CFSTR_VALUE;
		CFDictionarySetValue(attributes,CFSTR("org_iau_FITS_OBSERVER"),CFStr_value);

		result = fits_read_key(fptr,TSTRING,"ORIGIN",value,NULL,&status);
		CHECK_RETURN_AND_SET_CFSTR_VALUE;
		CFDictionarySetValue(attributes,CFSTR("org_iau_FITS_ORIGIN"),CFStr_value);

		result = fits_read_key(fptr,TSTRING,"PROGRAM",value,NULL,&status);
		CHECK_RETURN_AND_SET_CFSTR_VALUE;
		CFDictionarySetValue(attributes,CFSTR("org_iau_FITS_PROGRAM"),CFStr_value);

		result = fits_read_key(fptr,TSTRING,"RA",value,NULL,&status);
		CHECK_RETURN_AND_SET_CFSTR_VALUE;
		CFDictionarySetValue(attributes,CFSTR("org_iau_FITS_RA"),CFStr_value);

		result = fits_read_key(fptr,TSTRING,"TELESCOP",value,NULL,&status);
		CHECK_RETURN_AND_SET_CFSTR_VALUE;
		CFDictionarySetValue(attributes,CFSTR("org_iau_FITS_TELESCOP"),CFStr_value);


		
		// return YES so that the attributes are imported
		success=TRUE;
		
		fits_close_file(fptr, &status);

    }
    [pool release];
    return success;
	
};
